#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "Script.h"
#include "Log.h"

NS_USING;

Script::Script():
	pState(NULL)
{

}

Script::~Script()
{
	if(pState){
		lua_close(pState);
	}
}

bool Script::open(const char *filename)
{
	lua_State *L = lua_open();
	if(luaL_dofile(L, filename)){
		std::cout << "parse config file error=" << lua_tostring(L, -1) << std::endl;
		lua_close(L);
		return false;
	}
	
	pState = L;
	return true;
}

int Script::readInt(const char *key)
{
	lua_getglobal(pState, key);
	int n = lua_tonumber(pState, -1);
	lua_pop(pState, 1);
	
	return n;
}

bool Script::readBoolean(const char *key)
{
	int n = readInt(key);
	return n ? true : false;
}

const char* Script::readString(const char *key)
{
	lua_getglobal(pState, key);
	const char *str = lua_tostring(pState, -1);
	lua_pop(pState, 1);
	return str;
}